<template>
  <div class="more-module-manage">
    <TabsIframe
      :menuList="applicationList"
      :menuActive="menuActive"
      :menuIndex="menuIndex"
      @menuClick="oppenTab"
    />
  </div>
</template>
<script>
import { getMenuList } from '@/api'
import projectInfoApi from '../../api/projectInfo';

export default {
  name: "more-module-manage",

  data() {
    return {
      applicationList: [],
      menuActive: {},
      projectKey: '',
      spaceKey: '',
      organizationKey: '',
      menuIndex: 0
    };
  },
  mounted() {
    this.projectKey = localStorage.getItem('projectKey') || this.$route.query.projectKey
    this.getList();
  },
  methods: {
    async oppenTab(item) {
      if (item.type == 'redirect') {
        let menuUrl = await this.$menuRedirectBaseUrl(item)
        // 外链不切换菜单
        // if (item.PREINDEX !== undefined) {
        //   this.menuIndex = item.PREINDEX
        // }
        if (menuUrl.indexOf('projectKey=' < 0)) {
          menuUrl = menuUrl + '&projectKey=' + this.projectKey
        }

        if (menuUrl.indexOf('spaceKey=' < 0)) {
          menuUrl = menuUrl + '&spaceKey=' + this.$route.query.spaceKey
        }
        
        this.menuActive = {}
        window.open(menuUrl)
      } else {
        let param = JSON.parse(JSON.stringify(item));
        let menuUrl = this.changeDevHost(param.menuUrl);
        let separator = menuUrl.indexOf('?') > -1 ? '&' : '?';
        let token = param.token || localStorage.getItem('token')

        menuUrl = menuUrl + separator + 'token=' + token + '&menuId=' + item.primaryKey

        if (menuUrl.indexOf('projectKey=' < 0)) {
          menuUrl = menuUrl + '&projectKey=' + this.projectKey
        }

        if (menuUrl.indexOf('spaceKey=' < 0)) {
          menuUrl = menuUrl + '&spaceKey=' + this.$route.query.spaceKey
        }

        param.menuUrl = menuUrl
        this.menuActive = param
      }
    },
    changeDevHost (url) {
      let urlHost = url.match(/^(?:[^\/]|\/\/)*/)
      // 开发环境替换域名为本地ip
      if (process.env.NODE_ENV === 'development' && window.devHostMap && urlHost && window.devHostMap[urlHost]) {
        url = url.replace(urlHost, window.devHostMap[urlHost])
      }
      return url
    },
    async getList() {
      let res = await getMenuList({
        parentId: this.$route.query.menuId,
        typeList:['normal','redirect'],
      });
      this.applicationList = res.rows || []

      if (this.$route.query.authCode) {
        for (let i = 0,len = this.applicationList.length; i < len; i++) {
          if (this.applicationList[i].authCode === this.$route.query.authCode) {
            this.menuIndex = i
            break
          }
        }
      }

      this.oppenTab(this.applicationList[this.menuIndex])
    },
  },
};
</script>


<style lang="scss" scoped>
.more-module-manage {
  height: 100%;
  width: 100%;

  .yj-rent-iframe-tabs {
    padding: 20px;

    ::v-deep .left {
      width: 120px;
      margin-right: 10px;
    }

    ::v-deep .el-menu {
      border: none;
    }

    ::v-deep .el-menu .el-menu-item {
      text-align: center;
    }

    ::v-deep .el-menu .is-active {
      background: rgba(51,132,254,0.1);
    }
  }
}
</style>
